In [ ]:
%%HTML
<style>
.container { width:100% }
</style>
In [ ]:
import gzip
import pickle
import random
import numpy as np
import sklearn.svm as svm
The function $\texttt{load_data}()$ returns a pair of the form $$ (\texttt{training_data}, \texttt{test_data}) $$ where
In [ ]:
def load_data():
with gzip.open('mnist.pkl.gz', 'rb') as f:
train, validate, test = pickle.load(f, encoding="latin1")
X_train = np.array([np.reshape(x, (784, )) for x in train[0]])
X_test = np.array([np.reshape(x, (784, )) for x in test [0]])
Y_train = np.array(train[1])
Y_test = np.array(test [1])
return (X_train, X_test, Y_train, Y_test)
In [ ]:
X_train, X_test, Y_train, Y_test = load_data()
Let us see what we have read:
In [ ]:
X_train.shape, X_test.shape, Y_train.shape, Y_test.shape
We define a support vector machine with Gaussian kernel.
In [ ]:
M = svm.SVC(kernel='rbf', gamma=0.05, C=5)
In [ ]:
%%time
M.fit(X_train, Y_train)
In [ ]:
M.score(X_train, Y_train)
In [ ]:
M.score(X_test, Y_test)
In [ ]: